﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Library.Linq
{
    public static class RegularExpressions
    {
        public static IEnumerable<string> WhereLike(this IEnumerable<string> source, string pattern)
        {
            Regex regex = new Regex(pattern);

            foreach (var item in source)
                if (regex.IsMatch(item))
                    yield return item;
        }
        public static IEnumerable<string> WhereNotLike(this IEnumerable<string> source, string pattern)
        {
            Regex regex = new Regex(pattern);

            foreach (var item in source)
                if (!regex.IsMatch(item))
                    yield return item;
        }

        public static IEnumerable<TSource> WhereLike<TSource>(this IEnumerable<TSource> source, Func<TSource, string> keySelector, string pattern)
        {
            Regex regex = new Regex(pattern);

            foreach (var item in source)
                if (regex.IsMatch(keySelector(item)))
                    yield return item;
        }
        public static IEnumerable<TSource> WhereNotLike<TSource>(this IEnumerable<TSource> source, Func<TSource, string> keySelector, string pattern)
        {
            Regex regex = new Regex(pattern);

            foreach (var item in source)
                if (!regex.IsMatch(keySelector(item)))
                    yield return item;
        }
    }
}
